package mysql

import (
	"gorm.io/gorm"
	"work/workrpc/workrpc"
)

type Book struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);comment:用户Id"`
	StoryId int64 `gorm:"type:int(11);comment:小说Id"`
}

func (b *Book) BookAdd(db *gorm.DB) error {
	return db.Debug().Create(b).Error
}

func (b *Book) BookDel(db *gorm.DB, in *workrpc.BookDelRequest) error {
	return db.Debug().Where("books.id=?", in.BookId).Delete(b).Error
}
func BookList(db *gorm.DB, in *workrpc.BookListRequest) ([]*workrpc.BookList, error) {
	var list []*workrpc.BookList
	//SELECT
	//  nick_name,
	//  head_image,
	//  title
	//FROM
	//  books
	//  LEFT JOIN users ON books.user_id = users.id
	//  LEFT JOIN stories ON books.story_id = stories.id
	db.Debug().Model(&Book{}).Select(
		"nick_name",
		"head_image",
		"title",
	).Joins("LEFT JOIN users ON books.user_id = users.id").
		Joins("LEFT JOIN stories ON books.story_id = stories.id").Where("users.id=?", in.UserId).Find(&list)

	return list, err
}
